package com.srd.phone.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.shengrongdai.srd.R;
import com.srd.phone.activity.RetrievePwdResetActivity;
import com.srd.phone.bean.ResultInfo;
import com.srd.phone.mvp.presenters.RetrievePwdPresenter;
import com.srd.phone.utils.Constants;

/**
 *
 */
public class RetrievePwdPhoneFragment extends BaseFragment implements View.OnClickListener {

    private View rootView;
    private EditText et_phone;
    private EditText et_verify;
    private TextView tv_warn;
    private Button bt_get_code;
    private Button bt_next;

    private String validUserUrl = Constants.BASEURL + "loginapp/validUser"; // 验证用户是否存在
    private String phCodeUrl = Constants.BASEURL + "loginapp/phCode"; //请求手机验证码接口，
    private String validCodeUrl = Constants.BASEURL + "loginapp/validCode"; //点击下一步，验证输入的验证码

    private RetrievePwdPresenter retrievePwdPresenter;
    private boolean isRequestPhone = true; //表示phone验证

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_retrieve_phone_pwd, container, false);
            initView();
            initData();
        }
        return rootView;
    }

    @Override
    public void initView() {
        super.initView();
        et_phone = (EditText) rootView.findViewById(R.id.et_phone);
        et_verify = (EditText) rootView.findViewById(R.id.et_verify);
        tv_warn = (TextView) rootView.findViewById(R.id.tv_warn);
        bt_get_code = (Button) rootView.findViewById(R.id.bt_get_code);
        bt_get_code.setOnClickListener(this);
        bt_next = (Button) rootView.findViewById(R.id.bt_next);
        bt_next.setOnClickListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new RetrievePwdPresenter(requestQueue, this);
        retrievePwdPresenter = (RetrievePwdPresenter)presenter;
        setUrlArrs(validUserUrl,phCodeUrl,validCodeUrl);
    }


    //用户是否存在验证
    private void checkUser() {
        String phone = et_phone.getText().toString().trim();
        String value = retrievePwdPresenter.checkLocalUser(isRequestPhone, phone, "");
        if (TextUtils.isEmpty(value)) {
            retrievePwdPresenter.checkUserExists(validUserUrl, phone);
            tv_warn.setVisibility(View.INVISIBLE);
        } else {
            tv_warn.setText(value);
            tv_warn.setVisibility(View.VISIBLE);
        }
    }

    //请求手机或者邮箱验证码
    private void sendCheckCode() {
        String phone = et_phone.getText().toString().trim();
        if (isRequestPhone) {
            retrievePwdPresenter.sendCheckCode(phCodeUrl, phone);
        }
        bt_get_code.setEnabled(false);
        downTimer.start();
    }

    CountDownTimer downTimer = new CountDownTimer(1000 * 60, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            bt_get_code.setText("剩余" + millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            bt_get_code.setEnabled(true);
            bt_get_code.setText("重新获取");
        }
    };

    //验证提交
    private void checkCode() {
        String phone = et_phone.getText().toString().trim();
        String code = et_verify.getText().toString().trim();
        String value = retrievePwdPresenter.checkLocalCode(isRequestPhone, phone, "", code);
        if (TextUtils.isEmpty(value)) {
            retrievePwdPresenter.checkCode(validCodeUrl, phone, code);
            tv_warn.setVisibility(View.INVISIBLE);
        } else {
            tv_warn.setText(value);
            tv_warn.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        ResultInfo resultInfo = ResultInfo.parse(data);
        if (tag.equalsIgnoreCase(validUserUrl) && resultInfo.code == -101) {
            sendCheckCode();
            return;
        }
        if (tag.equalsIgnoreCase(phCodeUrl) && resultInfo.code == -101) {
            Toast.makeText(mContext, "验证码发送成功", Toast.LENGTH_SHORT).show();
            return;
        }
        if (tag.equalsIgnoreCase(validCodeUrl) && resultInfo.code == -101) {
            startActivity(new Intent(mContext, RetrievePwdResetActivity.class));
            return;
        }
        tv_warn.setVisibility(View.VISIBLE);
        tv_warn.setText(resultInfo.msg);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_common_right:
                break;
            case R.id.bt_get_code:
                checkUser();
                break;
            case R.id.bt_next:
                checkCode();
                break;
        }
    }

    @Override
    public void onDestroy() {
        downTimer.cancel();
        super.onDestroy();
    }
}